<?php

class edit_reservednumbers
{
	var $driverid;
	var $number;
	var $drivername;
	var $champid;
	
	var $numberOptions;
	var $driverOptions;
	
	var $add_record;
	var $valid;

	function edit_reservednumbers($add_record, $champid, $record='')
	{
		global $db, $lang;
		if($add_record)
		{
			$this->add_record = true;
			$this->driverid = 0;
			$this->number = 0;
			$this->drivername = '';
			$this->champid = $champid;
			
			$this->driverOptions = '';
			$q_drivers = 'SELECT u.user_id, u.username, pf_drivername as user_drivername 
						  FROM ' . USERS_TABLE . ' u, '. USERSGROUPS_TABLE . ' g, ' . DRIVERNAME_TABLE . ' d 
						  WHERE u.user_id = d.user_id  AND g.user_id = u.user_id AND g.group_id = 3 AND u.user_id NOT IN ' .
					'(SELECT cs_driver FROM ' . CHAMPSIGNUPS_TABLE . ' WHERE cs_champ = ' . $this->champid . ') AND u.user_id NOT IN ' .
					'(SELECT r_driver FROM ' . RESERVEDNUMBERS_TABLE . ' WHERE r_champ = ' . $this->champid . ') ORDER BY pf_drivername ASC';	
			
			$result = $db->sql_query($q_drivers);
			while($row = $db->sql_fetchrow($result)) 
			{
				$this->driverOptions = $this->driverOptions . 
						'<option value="'.$row['user_id'].'" >' . $row['user_drivername'] . 
											' (' . $row['username'] . ')</option>';
			}
						
			$this->numberOptions = '';
			$q_numbers = 'SELECT car_no FROM ' . CARNUMBERS_TABLE . ' WHERE car_no NOT IN ' .
				'(SELECT cs_no FROM ' . CHAMPSIGNUPS_TABLE . ' WHERE cs_champ = ' . $this->champid . ') AND car_no NOT IN' .
				'(SELECT r_no FROM ' . RESERVEDNUMBERS_TABLE . ' WHERE r_champ = ' . $this->champid . ') ORDER BY car_no';
			$result = $db->sql_query($q_numbers);
			while($row = $db->sql_fetchrow($result)) 
			{
				$this->numberOptions = $this->numberOptions . 
						'<option value="'.$row['car_no'].'">'  . $row['car_no'] . '</option>';
			}
			
			$this->valid = true;
			
		}
		else if(!$add_record)
		{
			$this->add_record = false;
			$this->driverid = $record['r_driver'];
			$this->number = $record['r_no'];
			$this->drivername = $record['user_drivername'];
			$this->champid = $champid;
			
            $this->driverOptions = '<option value="'.$this->driverid.'" SELECTED>'  . $this->drivername . 
                                        ' (' . $record['username'] . ')</option>';
			$this->numberOptions = '<option value="'. $this->number  . '" SELECTED>' . $this->number  . '</option>';
						
			$this->valid = true;
		}
		else
		{
			$this->valid = false;
		}
        
	}
		
	function render()
	{		
		global $skin_dir, $lang;
		
		$submitText = $this->add_record ? $lang['AddButton'] : $lang['RemoveButton'];
		$submitValue = $this->add_record ? 'addRecord' : 'removeRecord';
		
		$champid = $this->champid;
		$driverOptions = $this->driverOptions;
		$numberOptions = $this->numberOptions;
			 
    	include $skin_dir . 'admin/editreservednumber.tpl';
	}
}

class reservednumbers
{
	var $mess;
	var $champid;
        
	var $toRender = Array();

	function reservednumbers($champid)
	{
		$mess = '';
		$this->champid = $champid;
		if(isset($_POST['removeRecord']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']))
		{
			$this->doAdd();
		}
		$this->setupEdit();
		$this->setupAdd();
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		foreach( $this->toRender as $key => $value){
			$value->render();
		}
		
	}
	
	function doDelete()
	{
		global $db, $lang;
		
		$qUpdateChamps = 'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' WHERE user_id = ' . $_POST['driver'];
		$result = $db->sql_query($qUpdateChamps);
		if($row = $db->sql_fetchrow($result))
		{
			//get the record
			$qDel = "delete from ". RESERVEDNUMBERS_TABLE . " Where r_champ = " . $this->champid . " AND r_driver = " . $_POST['driver'];
			$db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			
			$qSel = 'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' WHERE user_id = ' . $_POST['driver'];
			$qUpd = "insert into  ". RESERVEDNUMBERS_TABLE . " values(" .
							 $_POST['number'] . ", " . $_POST['driver'] . ", "  . $this->champid . " )";
			$result = $db->sql_query($qSel);
			if($row = $db->sql_fetchrow($result))
			{
				$db->sql_query($qUpd);
				$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
			}
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields()
	{
		$valid = isset($_POST['number']) && is_numeric($_POST['number'])
				&& isset($_POST['driver']) && is_numeric($_POST['driver']);
		
		return $valid;	  
	}
	
	function setupEdit()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('includes/renderInclude.php');
		
		        
        //get the record
		
		$q = "SELECT *, pf_drivername as user_drivername 
			  FROM " . RESERVEDNUMBERS_TABLE . ", " . USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d 
			  WHERE u.user_id = d.user_id AND u.user_id = r_driver AND r_champ = " . 
                   $this->champid . " ORDER BY r_no ASC";
		$result = $db->sql_query($q);
                
		$introMess = false;
		
		while($row = $db->sql_fetchrow($result)) 
		{
			if(!$introMess)
			{
				$this->toRender[] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
				$this->toRender[] = new renderInclude('tpl/tableStart.tpl');
				$introMess = true;
			}
			
			$this->toRender[] = new edit_reservednumbers(false, $this->champid, $row);
		}
		
		if($introMess)
		{
			$this->toRender[] = new renderInclude('tpl/tableEnd.tpl');
		}
	}

	function setupAdd()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('includes/renderInclude.php');
		
		$this->toRender[] = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$this->toRender[] = new renderInclude('tpl/tableStart.tpl');
		$this->toRender[] = new edit_reservednumbers(true, $this->champid);
		$this->toRender[] = new renderInclude('tpl/tableEnd.tpl');
	}

	
}
?>